package com.dliu.javadesign.structure.composite;

import lombok.Data;
import lombok.experimental.Accessors;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yanglei
 * date 2021/3/30 17:38
 */
public interface Menu {
    void print();
}

@Data
@Accessors(chain = true)
class Menus implements Menu {
    private Integer level;
    private String name;
    private List<Menus> menus;

    @Override
    public void print() {
        System.out.println(this.toString());
        menus.forEach(System.out::println);
    }

    @Override
    public String toString() {
        return "Menus{" +
                "level=" + level +
                ", name='" + name + '\'' +
                '}';
    }

    public static void main(String[] args) {
        Menus menus = new Menus();
        menus.setName("根目录");
        menus.setLevel(0);

        List<Menus> menusList = new ArrayList<>();
        menusList.add(new Menus().setName("一级目录1").setLevel(1));
        menusList.add(new Menus().setName("一级目录2").setLevel(1));
        menus.setMenus(menusList);

        menus.print();
    }
}
